package com.whpost.service.dao.t;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whpost.service.entity.t.TMenu;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface TMenuDao extends BaseMapper<TMenu> {

    // 获取所有菜单
    @Select("select id from t_menu order by xh")
    List<String> getAllMenuId();

    @Select("select id,nvl(pid,0) as pid,name,zjm,jb,href,ico,state,xh,qxdm from t_menu where ${ew.sqlSegment}")
    @Results(id="tMenuMap",value = {
            @Result(column="pid",property="pMenu",
                    one=@One(
                            select="com.whpost.service.dao.t.TMenuDao.selectById")
            ),
            @Result(column="pid",property="pid")
    })
    IPage<TMenu> getTMenuTreeTable(Page<TMenu> page,@Param("ew") QueryWrapper wrapper);

    @Select("select * from t_menu where id=#{id}")
    @ResultMap(value="tMenuMap")
    TMenu getTMenuById(String id);

    // 下级菜单数量
    @Select("select count(*) as sl from t_menu where pid=#{pid}")
    int getXjMenuSl(Long pid);

    // 获取父code
    @Select("select pid from t_menu where id=#{id}")
    String getPcodeById(Long id);

    // 获取菜单名称
    @Select("select name from t_menu where id=#{id}")
    String getNameById(Long id);

    // 获取下级数量
    @Select("select count(id) from t_menu where pid=#{id}")
    int countSon(Long id);

    // 获取二级菜单
    @Select("select * from t_menu where pid=#{id} and jb='2' order by xh asc")
    List<TMenu> getXjMenu2(Long id);

    @Update("update t_menu set pid = null where id=#{id}")
    int setPidNull(String id);

}